package com.wangge.concurrentTest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class CountDown {
  static volatile int index = 0;

  public static Long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(concurrency);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(concurrency);
    for (int i = 0; i < concurrency; i++) {
      executor.execute(new Runnable() {

        @Override
        public void run() {
          ready.countDown();
          try {
            start.await();
            action.run();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } finally {
            index++;
            done.countDown();
            System.out.println(index);
          }
        }

      });
    }
    ready.await();
    Long startNanos = System.nanoTime();
    start.countDown();
    done.await();
    return System.nanoTime() - startNanos;

  }

  public static void main(String[] args) throws InterruptedException {
    System.out.println("用时" + CountDown.time(Executors.newCachedThreadPool(), 3, () -> {
      System.out.println("123");
    }));
    
  List l=new  ArrayList<>();
  l.remove(2);
  }
}
